package cn.haona.game.simb.item.weapon;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.HashMap;

import cn.haona.db.RelevanceBean;
import cn.haona.game.simb.comm.util.Util;
import cn.haona.game.simb.item.ItemImpl;
import cn.haona.game.simb.item.PersistenceItem;
import cn.haona.game.simb.object.Persistence;
import cn.haona.game.simb.object.bio.PersistenceHuman;
import cn.haona.game.simb.object.bio.User.UserPO;
import cn.haona.game.simb.object.god.MainManager;

public class Weapon extends ItemImpl {

	public void initPO() {
		if (pO == null) {
			pO = new WeaponPO();
		}
	}
	
	public WeaponPO getWeaponPO()
	{
		return (WeaponPO)pO;
	}

	public class WeaponPO extends PersistenceItem implements Serializable {

		final public static String TABLE_NAME = "OPT_WEAPON";
		final public static String ID = "WEP_ID";
		final public static String NAME = "WEP_NAME";
		final public static String WEP_TYPE = "WEP_TYPE";
		final public static String MIN_EFFECT = "WEP_MIN_EFFECT";
		final public static String MAX_EFFECT = "WEP_MAX_EFFECT";
		final public static String SUCCESS_RATE = "WEP_SUCCESS_RATE";
		final public static String PRICE = "WEP_PRICE";
		final public static String NEED_EXP = "WEP_NEED_EXP";
		final public static String DESC = "WEP_DESC";
		final public static String AMOUNT_AMMUNTION = "WEP_AMOUNT_AMMUNTION";
		final public static String SOLDINSHOP = "WEP_SOLDINSHOP";
		private int weaponType;
		
		private int maxEffect;
		private int minEffect;
		private int successRate;
		private int needExp;
		private String desc;
		private int amountAmmuntion;
		/**
		 * 
		 */
		private static final long serialVersionUID = 6871164322342057385L;

		
		public boolean persisMe() {
			// TODO Auto-generated method stub
			return false;
		}
		
		public Long persisMeFirstTime() {

			Connection conn = MainManager.getInstance().getPersistenceManager()
					.getDB_Connection();
			PreparedStatement ps = null;
			String sql = "insert into " + TABLE_NAME + " ("
					+ NAME + "," 
					+ WEP_TYPE + ","
					+ MIN_EFFECT + "," 
					+ MAX_EFFECT + ","
					+ SUCCESS_RATE+ "," 
					
					+ PRICE+ "," 
					+ NEED_EXP+ "," 
					+ DESC+ "," 
					+ AMOUNT_AMMUNTION+ "," 
					+ SOLDINSHOP+ "," 
					
					+ ADDEDON+ "," 
					+ UPDATEON+ "," 
					+ STATUS
					+ 
					") values (" +
					"?,?,?,?,?," +
					"?,?,?,?,?," +
					"?,?,?)";
			try {
				ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);

				ps.setString(1, getName());
				ps.setInt(2, getWeaponType());
				ps.setInt(3, getMinEffect());
				ps.setInt(4, getMaxEffect());
				ps.setInt(5, getSuccessRate());
				
				ps.setInt(6, getPrice());
				ps.setInt(7, getNeedExp());
				ps.setString(8, getDesc());
				ps.setInt(9, getAmountAmmuntion());
				ps.setInt(10, getIsSoldInShop());
				
				ps.setTimestamp(11, Util
						.getTimestamp(System.currentTimeMillis()));
				ps.setTimestamp(12, Util
						.getTimestamp(System.currentTimeMillis()));
				ps.setInt(13, 0);
				ps.executeUpdate();
				ResultSet rs = ps.getGeneratedKeys();
				if (rs.next()) {
					long lID = rs.getInt(1);
					return lID;
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (conn != null) {
						conn.close();
					}
					if (ps != null) {
						ps.close();
					}

				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			return -1L;
		}

		public boolean cacheMe() {
			// TODO Auto-generated method stub
			return false;
		}

		public Persistence updateMe() {

			Connection conn = MainManager.getInstance().getPersistenceManager()
					.getDB_Connection();
			PreparedStatement ps = null;
			ResultSet rs = null;
			String sql = "select * from " + getTableName() + " where " + ID
					+ "=?";
			try {
				ps = conn.prepareStatement(sql);
				ps.setLong(1, getID());
				rs = ps.executeQuery();
				if(rs.next())
				{
					setAddedDate(rs.getTimestamp(ADDEDON));
					setAmountAmmuntion(rs.getInt(AMOUNT_AMMUNTION));
					setDesc(rs.getString(DESC));
					setIsSoldInShop(rs.getInt(SOLDINSHOP));
					setMaxEffect(rs.getInt(MAX_EFFECT));
					setMinEffect(rs.getInt(MIN_EFFECT));
					setName(rs.getString(NAME));
					setNeedExp(rs.getInt(NEED_EXP));
					setPrice(rs.getInt(PRICE));
					setStatus(rs.getInt(STATUS));
					setSuccessRate(rs.getInt(SUCCESS_RATE));
//					setUpDateDate(rs.getTimestamp(UPDATEON));
//					setWeaponType(rs.getInt(WEP_TYPE));
				}
				return this;

			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (rs != null) {
						rs.close();
					}
					if (ps != null) {
						ps.close();
					}
					if (conn != null) {
						conn.close();
					}

				} catch (Exception e) {
					{
						e.printStackTrace();
					}
				}
			}
			// PrepareStatement ps=conn.prepareStatement(sql);
			// ps.setString(1,"56789"); //设定参数值
			// ResulrSet rs=ps.executeQuery(); //获得查询结果

			return null;
		}

		//得到主键名
		public String getPrimaryKeyName()
		{
			return ID;
		}
		
		public int getMaxEffect() {
			return maxEffect;
		}

		public void setMaxEffect(int maxEffect) {
			this.maxEffect = maxEffect;
		}

		public int getMinEffect() {
			return minEffect;
		}

		public void setMinEffect(int minEffect) {
			this.minEffect = minEffect;
		}

		public int getWeaponType() {
			return weaponType;
		}

		public void setWeaponType(int weaponType) {
			this.weaponType = weaponType;
		}

		@Override
		public int getPrice() {
			return price;
		}

		@Override
		public void setPrice(int price) {
			this.price=price;
		}


		public void setID(long id) {
			this.id=id;

		}

		public long getID() {
			return id;
		}

		@Override
		public int getIsSoldInShop() {
			return isSoldInShop;
		}

		@Override
		public void setIsSoldInShop(int isSoldInShop) {
			this.isSoldInShop=isSoldInShop;
		}

		public int getSuccessRate() {
			return successRate;
		}

		public void setSuccessRate(int successRate) {
			this.successRate = successRate;
		}

		public int getNeedExp() {
			return needExp;
		}

		public void setNeedExp(int needExp) {
			this.needExp = needExp;
		}

		public String getDesc() {
			return desc;
		}

		public void setDesc(String desc) {
			this.desc = desc;
		}

		public int getAmountAmmuntion() {
			return amountAmmuntion;
		}

		public void setAmountAmmuntion(int amountAmmuntion) {
			this.amountAmmuntion = amountAmmuntion;
		}

		public HashMap<String, RelevanceBean> getReflectRelevance() {
			// TODO Auto-generated method stub
			return null;
		}



	}

}
